package com.lcx.plugin.neteaseCloud.component.gd;

import com.intellij.openapi.ui.DialogWrapper;
import com.lcx.plugin.neteaseCloud.component.handle.SongQueueHandle;
import com.lcx.plugin.neteaseCloud.component.toolwindow.CustomNodeRenderer;
import com.lcx.plugin.neteaseCloud.constant.IconConstant;
import com.lcx.plugin.neteaseCloud.music.entity.MusicListEntity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

/**
 * @author wengyongcheng
 * @since 2020/3/7 10:22 下午
 */
public class SongListDialogWrapper extends DialogWrapper {

    private MusicListEntity musicListEntity;

    public SongListDialogWrapper(MusicListEntity musicListEntity) {
        super(true);
        this.musicListEntity = musicListEntity;
        init();
        setTitle("歌单-详情(1/1)");
    }

    @Nullable
    @Override
    protected JComponent createCenterPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setPreferredSize(new Dimension(200, 200));

        String[] items = {musicListEntity.getName(), "添加到队列"};
        JList<String> jList = new JList<>(items);
        jList.addListSelectionListener(e -> {
            // 设置只有释放鼠标时才触发
            if (!jList.getValueIsAdjusting()) {
                SongQueueHandle.handle(jList.getSelectedIndex(), jList.getSelectedValue(), musicListEntity);
            }
        });
        scrollPane.setViewportView(jList);
        panel.add(scrollPane);

        /**
         * 重点：设置节点渲染器
         */
        Icon[] icons = new Icon[]{IconConstant.NOTE, IconConstant.ADD};
        Icon[] selectIcons = new Icon[]{IconConstant.NOTE_SELECT, IconConstant.ADD_SELECT};
        jList.setCellRenderer(new CustomNodeRenderer(icons, selectIcons));
        return panel;
    }

    /**
     * 覆盖默认的ok/cancel按钮
     *
     * @return
     */
    @NotNull
    @Override
    protected Action[] createActions() {
        // 设置默认的焦点按钮
        return new Action[]{null, null};
    }

}
